package controler;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import dao.GetCnn;

/**
 * Servlet implementation class quanlynhanvien
 */
@WebServlet("/quanlynhanvien")
public class quanlynhanvien extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public quanlynhanvien() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @throws UnsupportedEncodingException 
	 * @throws SQLException 
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException, SQLException{
    	String manhanvien = request.getParameter("manhanvien");
    	String tennhanvien = request.getParameter("tennhanvien");
    	tennhanvien = new String(tennhanvien.getBytes("8859_1"),"UTF8");
    	String diachi = request.getParameter("diachi");
    	diachi = new String (diachi.getBytes("8859_1"),"UTF8");
    	String matkhau = request.getParameter("matkhau");
    	String ngayvl = request.getParameter("ngayvl");
    	String luong = request.getParameter("luong");
    	String dienthoai = request.getParameter("dienthoai");
    	Connection cnn = GetCnn.getCnn();
		Statement stat = cnn.createStatement();
    	String option = request.getParameter("option");
    	String sSQL ="";
    	if(option.equals("addnew")){
    		sSQL = "insert into NHANVIEN values('"+manhanvien+"',N'"+tennhanvien+"',N'"+diachi+"','"+matkhau+"','"+ngayvl+"','"+luong+"','"+dienthoai+"')";
    		stat.executeUpdate(sSQL);
    	}
    	
    	if(option.equals("edit")){
    		sSQL = "update NHANVIEN set TENNHANVIEN = N'"+tennhanvien+"',DIACHI = N'"+diachi+"',MATKHAU = '"+matkhau+"',NGAYVL = '"+ngayvl+"',LUONG = '"+luong+"',DIENTHOAI = '"+dienthoai+"' where MANHANVIEN ='"+manhanvien+"'";
    		stat.executeUpdate(sSQL);
    	}
    	try {
			response.sendRedirect("adminnhanvien");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }
    
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		try {
			processRequest(request,response);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		try {
			processRequest(request,response);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
